package com.kunbo.cn.controller.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.entity.system.BusinessCode;
import com.kunbo.cn.service.system.BusinessCodeService;
import com.kunbo.cn.util.date.SDate;
/**
 * 
 * <p>Title: AutoCreateCode</p>
 * <p>Description:自动生成编码 </p>
 * <p>Company: www.kunbo.cn</p> 
 * @author	yang'f
 * @date	2019年1月21日下午4:43:37
 * @version 1.0
 */
import com.kunbo.cn.util.retu.ReturnBean;
@RestController
public class GetOrderNumberController {
	
	@Autowired
	private BusinessCodeService businessCodeService;
	@RequestMapping("/generateNumber")
	public String generateCode(@RequestParam("businessTableName")String tableName) {
		String prex = "";
		if(tableName.equals("bussiness_purchasing_order_manager")) {
			prex = "CGDD";
		}else if(tableName.equals("business_purchase_waybill")) {
			prex = "CGYD";
		}else if(tableName.equals("business_notice_of_arrival")) {
			prex = "DHTZD";
		}else if(tableName.equals("db_business_purchase_pound_list")) {
			prex = "CGBD";
		}else if(tableName.equals("business_sale_order_manage")) {
			prex = "XSDD";
		}else if(tableName.equals("business_waybill_manage")) {//通道编码
			prex = "XSYD";
		}else if(tableName.equals("business_delivery_notice_manage")) {//客户编码
			prex = "THTZD";
		}else if(tableName.equals("db_business_sales_pound_list")) {//仓库编码
			prex = "XSBD";
		}else if(tableName.equals("vehicle_management")) { //车辆
			prex = "CL";
		}else if(tableName.equals("driver_management")) { // 司机
			prex = "SJ";
		}
		//1.先判断有没有更新对应的日期和内码
		String date = "" ;
		List<BusinessCode> list = businessCodeService.findAll();
		if(list.size()>0) {
			date = businessCodeService.findAll().get(0).getDate();
		}
		if(!date.equals(SDate.getSystemDateYMDToString())) {
			businessCodeService.updateNumberAndDate(1, SDate.getSystemDateYMDToString());
		}
		//根据类型和日期获取对应的的 内码
		Integer n = 1;
		String nStr = "";
		BusinessCode b = businessCodeService.findByTypeAndDate(prex, SDate.getSystemDateYMDToString());
		if(null==b) {
			return (""+System.currentTimeMillis()).substring(0, 5);
		}else {
			n = b.getNumber();
			
		}
		if(n>0 && n<10) {
			nStr ="000" +n;
		}else if(n>=10 && n<100) {
			nStr ="00" +n;
		}else if(n>=100 && n<1000) {
			nStr ="0" +n;
		}else {
			nStr ="" +n;
		}
		return prex+SDate.getSystemDateYMD2ToString()+nStr;
		
	}
	
	//数据保存成功后更新对应的内码
	@RequestMapping("/updateCode")
	public ReturnBean updateNumber(@RequestParam("businessTableName")String tableName) {
		String type = "";
		if(tableName.equals("bussiness_purchasing_order_manager")) {
			type = "CGDD";
		}else if(tableName.equals("business_purchase_waybill")) {
			type = "CGYD";
		}else if(tableName.equals("business_notice_of_arrival")) {
			type = "DHTZD";
		}else if(tableName.equals("db_business_purchase_pound_list")) {
			type = "CGBD";
		}else if(tableName.equals("business_sale_order_manage")) {
			type = "XSDD";
		}else if(tableName.equals("business_waybill_manage")) {//通道编码
			type = "XSYD";
		}else if(tableName.equals("business_delivery_notice_manage")) {//客户编码
			type = "THTZD";
		}else if(tableName.equals("db_business_sales_pound_list")) {//仓库编码
			type = "XSBD";
		}
		try {
			businessCodeService.update(type, SDate.getSystemDateYMDToString());
			return new ReturnBean(true,"更新成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ReturnBean(false,"更新失败");
		}
		
	}
	
	
	
	
	
	

}